package com.ruoyi.edu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.edu.domain.EduCourse;
import com.ruoyi.edu.domain.EduTeacher;
import com.ruoyi.edu.service.IEduCourseService;
import com.ruoyi.edu.service.IEduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/edu/nocheck")
public class FrontNoCheckController {

    @Autowired
    private IEduTeacherService eduTeacherService;

    @Autowired
    private IEduCourseService eduCourseService;

    /**
     * 获取首页热门教师列表
     * @return
     */
    @GetMapping("/getTeacherList")
    @Cacheable(value = "getTeacherList", key = "'teacher'", cacheManager = "redisExpire")
    public AjaxResult getTeacherList() {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.eq("level", 2);
        wrapper.orderByDesc("create_time");
        wrapper.last("limit 4");
        wrapper.select("id", "name", "level", "avatar");
        List<EduTeacher> list = eduTeacherService.list(wrapper);
        return AjaxResult.success("teacherList", list);
    }

    /**
     * 获取首页热门课程列表
     * @return
     */
    @GetMapping("/getCourseList")
    @Cacheable(value = "getCourseList", key = "'course'", cacheManager = "redisExpire")
    public AjaxResult getCourseList() {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("status", "Normal");
        wrapper.orderByDesc("view_count");
        wrapper.last("limit 4");
        wrapper.select("id", "cover" ,"title", "lesson_num", "price", "view_count");
        List<EduCourse> list = eduCourseService.list(wrapper);
        return AjaxResult.success("courseList", list);
    }

    /**
     * 根据教师id获取教师详细信息
     * @return
     */
    @GetMapping("/getTeacherInfo/{id}")
    @Cacheable(value = "getTeacherInfo", key = "'teacherid_'+#id", cacheManager = "redisExpire")
    public AjaxResult getTeacherInfo(@PathVariable String id) {
        EduTeacher teacher = eduTeacherService.getById(id);
        return AjaxResult.success(teacher);
    }

    /**
     * 根据教师id获取教师详细信息
     * @return
     */
    @GetMapping("/getCourseByTeacher/{id}")
    @Cacheable(value = "getCourseByTeacher", key = "'teachercourse_'+#id", cacheManager = "redisExpire")
    public AjaxResult getCourseByTeacher(@PathVariable String id) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("status", "Normal");
        wrapper.eq("teacher_id", id);
        wrapper.orderByDesc("view_count");
        wrapper.select("id", "cover" ,"title", "lesson_num", "price", "view_count");
        List<EduCourse> list = eduCourseService.list(wrapper);
        return AjaxResult.success("courseList", list);
    }
}
